package vn.vfriends.soffice.bean;

import java.io.Serializable;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.inject.Inject;
import org.omnifaces.util.Faces;
import org.primefaces.context.RequestContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import vn.vfriends.soffice.entity.Menu;
import vn.vfriends.soffice.service.MenuService;

/**
 *
 * @author chaungoctuan@gmail.com
 */
@ManagedBean
@ViewScoped
public class MenuBean implements Serializable {

    private static final Logger logger = LoggerFactory.getLogger(MenuBean.class);
    
    @Inject private MenuService menuService;
    
    private Menu[] selectedMenus;
    private Menu menu;
    
    private Integer menuId;

    public Menu[] getSelectedMenus() {
        return selectedMenus;
    }

    public void setSelectedMenus(Menu[] selectedMenus) {
        this.selectedMenus = selectedMenus;
    }

    public Menu getMenu() {
        return menu;
    }

    public void setMenu(Menu menu) {
        this.menu = menu;
    }

    public Integer getMenuId() {
        return menuId;
    }

    public void setMenuId(Integer menuId) {
        this.menuId = menuId;
    }

    public List<Menu> getMenuList() {
        return this.menuService.findAll();
    }
    
    public Menu.Code[] getMenuCodes() {
        return Menu.Code.values();
    }
    
    public Menu getMenu(String name) {
        return this.menuService.findByName(name);
    }
    
    public void preEditAction() {
        try {
            if(this.menu != null){
                Map<String,List<String>> params = new HashMap<String, List<String>>();
                params.put("menuId", Arrays.asList(this.menu.getId().toString()));
                RequestContext.getCurrentInstance().openDialog("edit", null, params);
            } else if (this.selectedMenus == null || this.selectedMenus.length == 0) {
                FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Thông báo", "Hãy chọn một menu");
                RequestContext.getCurrentInstance().showMessageInDialog(message);
            } else if (this.selectedMenus.length > 1) {
                FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Thông báo", "Chỉ chọn một menu");
                RequestContext.getCurrentInstance().showMessageInDialog(message);
            } else {
                this.menu = this.selectedMenus[0];
                Map<String,List<String>> params = new HashMap<String, List<String>>();
                params.put("menuId", Arrays.asList(this.menu.getId().toString()));
                RequestContext.getCurrentInstance().openDialog("edit", null, params);
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Thông báo", "Lỗi hệ thống");
            RequestContext.getCurrentInstance().showMessageInDialog(message);
        }
    }
    
    public void preRenderEditView() {
        this.menu = new Menu();
        this.menu = this.menuService.find(menuId);
        if(this.menu == null) {
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Thông báo", "Menu không tồn tại");
            Faces.getContext().addMessage(null, message);
        }
    }
    
    public void updateAction() {
        try {
            this.menuService.edit(this.menu);
            this.reset();
            RequestContext.getCurrentInstance().closeDialog("editDialog");
        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Thông báo", "Lỗi hệ thống");
            RequestContext.getCurrentInstance().showMessageInDialog(message);
        }
    }
    
    public void preNewAction() {
        RequestContext.getCurrentInstance().openDialog("new");
    }
    
    public void preRenderNewView() {
        this.menu = new Menu();
    }
    
    public void saveAction() {
        try {
            this.menuService.create(this.menu);
            this.reset();
            RequestContext.getCurrentInstance().closeDialog("newDialog");
        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Thông báo", "Lỗi hệ thống");
            RequestContext.getCurrentInstance().showMessageInDialog(message);
        }
    }
    
    public void preDeleteAction() {
        try {
            if ((this.menu == null) && (this.selectedMenus == null || this.selectedMenus.length == 0)) {
                RequestContext.getCurrentInstance().addCallbackParam("invalid", true);
                
                FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Thông báo", "Hãy chọn một hoặc nhiều menu");
                RequestContext.getCurrentInstance().showMessageInDialog(message);
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Thông báo", "Lỗi hệ thống");
            RequestContext.getCurrentInstance().showMessageInDialog(message);
        }
    }
    
    public void deleteAction() {
        try {
            if(this.menu != null) {
                this.menuService.remove(menu);
            }
            else {
                for(Menu selectedMenu : selectedMenus) {
                    this.menuService.remove(selectedMenu);
                }
            }
            this.reset();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Thông báo", "Lỗi hệ thống");
            RequestContext.getCurrentInstance().showMessageInDialog(message);
        }
    }
    
    private void reset() {
        this.menu = null;
        this.menuId = 0;
        this.selectedMenus = null;
    }
}
